package com.example.frank.test2.HttpPost;

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Frank on 2018/3/15.
 */

public class HttpPost{

    public static String getReultForHttpPost(String val, String result) throws IOException {
        //服务器  ：服务器项目  ：servlet名称
        String path = "http://39.106.192.16:3000/user/autodata";
        org.apache.http.client.methods.HttpPost httpPost = new org.apache.http.client.methods.HttpPost(path);
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("up_data", val));
        httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));

        try {
            HttpResponse response = new DefaultHttpClient().execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, HTTP.UTF_8);
            } else {
                result = "error";
            }
        } catch (Exception e) {
            result = e.toString();

        }
        return result;
    }

    public static String getBitMapResultForHttpPost(String val) throws IOException {
        String res;
        String path = "http://10.0.0.15:3000/user/received_qrcode";
        org.apache.http.client.methods.HttpPost httpPost = new org.apache.http.client.methods.HttpPost(path);
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("up_date", val));
        httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));


        try {
            HttpResponse response = new DefaultHttpClient().execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                res = EntityUtils.toString(entity, HTTP.UTF_8);
            } else {
                res = "error";
            }
        } catch (Exception e) {
            res = e.toString();
        }
        System.out.println(val+"  "+res);
        return res;
    }
}
